#!/bin/bash

# COAI 项目启动脚本

echo "======================================="
echo "  COAI 协同工具 - 启动脚本"
echo "======================================="

# 1. 启动基础设施
echo ""
echo "[1/4] 启动基础设施服务..."
docker-compose up -d

echo "等待服务启动..."
sleep 10

# 2. 构建后端服务
echo ""
echo "[2/4] 构建后端服务..."
cd backend
./gradlew clean build -x test
cd ..

# 3. 启动后端服务（按依赖顺序）
echo ""
echo "[3/4] 启动后端微服务..."

# 启动API网关
echo "启动 API Gateway (8080)..."
java -jar backend/api-gateway/build/libs/api-gateway-1.0.0.jar &
sleep 5

# 启动核心服务
echo "启动 User Service (8085)..."
java -jar backend/user-service/build/libs/user-service-1.0.0.jar &

echo "启动 AI Service (8082)..."
java -jar backend/ai-service/build/libs/ai-service-1.0.0.jar &

echo "启动 Message Service (8081)..."
java -jar backend/message-service/build/libs/message-service-1.0.0.jar &

echo "启动 File Service (8084)..."
java -jar backend/file-service/build/libs/file-service-1.0.0.jar &

echo "启动 RTC Service (8083)..."
java -jar backend/rtc-service/build/libs/rtc-service-1.0.0.jar &

sleep 10

# 启动ERP服务
echo "启动 Warehouse Service (8086)..."
java -jar backend/erp-warehouse/build/libs/erp-warehouse-1.0.0.jar &

echo "启动 Logistics Service (8087)..."
java -jar backend/erp-logistics/build/libs/erp-logistics-1.0.0.jar &

echo "启动 Procurement Service (8088)..."
java -jar backend/erp-procurement/build/libs/erp-procurement-1.0.0.jar &

echo "启动 Manufacturing Service (8089)..."
java -jar backend/erp-manufacturing/build/libs/erp-manufacturing-1.0.0.jar &

sleep 5

# 启动数字孪生服务
echo "启动 Digital Twin Service (8090)..."
java -jar backend/digital-twin/build/libs/digital-twin-1.0.0.jar &

# 4. 启动前端
echo ""
echo "[4/4] 启动前端服务..."
cd frontend
npm run dev &
cd ..

echo ""
echo "======================================="
echo "  所有服务已启动！"
echo "======================================="
echo ""
echo "服务访问地址："
echo "  - API网关:      http://localhost:8080"
echo "  - 前端Web:      http://localhost:3000"
echo "  - MinIO控制台:  http://localhost:9001"
echo ""
echo "数据库连接："
echo "  - PostgreSQL:   localhost:5432"
echo "  - MongoDB:      localhost:27017"
echo "  - Redis:        localhost:6379"
echo "  - Kafka:        localhost:9092"
echo "  - InfluxDB:     localhost:8086"
echo ""
echo "微服务端口："
echo "  - User Service:         8085"
echo "  - Message Service:      8081"
echo "  - AI Service:           8082"
echo "  - RTC Service:          8083"
echo "  - File Service:         8084"
echo "  - Warehouse Service:    8086"
echo "  - Logistics Service:    8087"
echo "  - Procurement Service:  8088"
echo "  - Manufacturing Service: 8089"
echo "  - Digital Twin Service: 8090"
echo ""
echo "按 Ctrl+C 停止所有服务"
echo "======================================="

wait
